package com.atguigu.controller;


import com.atguigu.entity.BaseBrand;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseBrandService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author hachimen
 * @since 2021-08-24
 */
@RestController
@RequestMapping("product/brand")
public class BaseBrandController {
    @Autowired
    private BaseBrandService baseBrandService;

    @Value("${fastdfs.prefix}")
    private String fastdfsPrefix;
    //查
    @GetMapping("queryBrandByPage/{page}/{limit}")
    public RetVal queryBrandByPage(@PathVariable Long page,@PathVariable Long limit){
        Page<BaseBrand> baseBrandPage = new Page<>(page,limit);
        baseBrandService.page(baseBrandPage,null);
        return RetVal.ok(baseBrandPage);
    }
    //增
    @PostMapping
    public RetVal add(@RequestBody BaseBrand baseBrand){
        baseBrandService.save(baseBrand);
        return RetVal.ok();
    }

    //改
    @PutMapping
    public RetVal update(@RequestBody BaseBrand baseBrand){
        baseBrandService.updateById(baseBrand);
        return RetVal.ok();
    }
    //查单个
    @GetMapping("{id}")
    public RetVal query(@PathVariable Long id){
        BaseBrand baseBrand = baseBrandService.getById(id);
        return RetVal.ok(baseBrand);
    }
    //查单个
    @GetMapping("getBrand/{id}")
    public BaseBrand getBrand(@PathVariable Long id){
        BaseBrand baseBrand = baseBrandService.getById(id);
        return baseBrand;
    }

    //查全部
    @GetMapping("getAllBrand")
    public RetVal getAllBrand(){
        List<BaseBrand> list = baseBrandService.list(null);
        return RetVal.ok(list);
    }
    //删
    @DeleteMapping("{id}")
    public RetVal delete(@PathVariable Long id){
        baseBrandService.removeById(id);
        return RetVal.ok();
    }

    //上传
    @RequestMapping("fileUpload")
    public RetVal upload(MultipartFile file) throws Exception {
        //需要一个配置文件告诉fastdfs在哪里
        String configFilePath = this.getClass().getResource("/tracker.conf").getFile();
        //初始化
        ClientGlobal.init(configFilePath);
        //创建trackerClient 客户端
        TrackerClient trackerClient = new TrackerClient();
        //用trackerClient获取连接
        TrackerServer connection = trackerClient.getConnection();
        //创建StorageClient1
        StorageClient1 storageClient1 = new StorageClient1(connection, null);
        //文件原始名称 aaa.jpg
        String originalFilename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);
        //对文件实现上传
        String path = storageClient1.upload_appender_file1(file.getBytes(), extension, null);
        System.out.println("文件访问路径:"+fastdfsPrefix+path);
        return RetVal.ok(fastdfsPrefix+path);
    }


}

